package com.panshi.hujin2.httpcrawler.util.gmail;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.panshi.hujin2.httpcrawler.domain.gmail.GmailAttachmentInfoBO;
import com.panshi.hujin2.httpcrawler.domain.gmail.GmailEmailBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * analysis gmail
 * @Author: wangxl
 * @Date: 2019/1/10 17:51
 */
public class GmailEmailJsonConvertUtil {

    private static Logger logger = LoggerFactory.getLogger(GmailEmailJsonConvertUtil.class);

    public static List<GmailEmailBO> convert(String jsonStr){

        logger.info("开始解析所有gmail邮件内容");

        List<GmailEmailBO> list = new ArrayList<>();

        JsonParser jsonParser = new JsonParser();

        JsonObject jsonObject = jsonParser.parse(jsonStr).getAsJsonObject();


        JsonArray emailArray = jsonObject.getAsJsonArray("2");

        GmailEmailBO gmailEmailBO = null;
        for(int i=0;i<emailArray.size();i++){

            gmailEmailBO = new GmailEmailBO();

            JsonObject element = emailArray.get(i).getAsJsonObject();

            // threadF
            String threadF = element.get("1").toString();
            threadF = threadF.replaceAll("\"","");
            gmailEmailBO.setThreadF(threadF);

            // emailTitle
            JsonObject emailTitleObject = element.getAsJsonObject("2").getAsJsonObject("1");
            String emailTitle = emailTitleObject.get("2").toString();
            emailTitle = emailTitle.replaceAll("\"","");
            gmailEmailBO.setEmailTitle(emailTitle);

            // th
            String th = emailTitleObject.get("16").toString();
            th = th.replaceAll("\"","");
            gmailEmailBO.setTh(th);

            //receiveDate
            String receiveDateLongStr = emailTitleObject.get("4").toString();
            receiveDateLongStr = receiveDateLongStr.replaceAll("\"","");
            gmailEmailBO.setReceiveDate(new Date(Long.valueOf(receiveDateLongStr)));

            //senderAddress
            JsonObject senderAddressObject = element.getAsJsonObject("2").getAsJsonArray("2").get(0).getAsJsonObject();
            JsonObject temp1 = senderAddressObject.getAsJsonObject("2");
            String senderAddress = temp1.get("2").toString();
            senderAddress = senderAddress.replaceAll("\"","");
            gmailEmailBO.setSenderAddress(senderAddress);

            // attachment content
            JsonObject attachmentObject = element.getAsJsonObject("2").getAsJsonArray("2").get(0).getAsJsonObject();
            JsonArray attachmentArray = attachmentObject.getAsJsonArray("13");
            if(attachmentArray != null && attachmentArray.size()>0){
                List<GmailAttachmentInfoBO> gmailAttachmentInfoBOList = new ArrayList<>();
                // attachment
                for(JsonElement ele:attachmentArray){
                    JsonObject temp = ele.getAsJsonObject().getAsJsonObject("4");

                    // email attachment file name
                    String attachmentFileName = temp.get("3").toString().replaceAll("\"","");
                    // email attachment file type
                    String attachmentFileType = temp.get("4").toString().replaceAll("\"","");
                    // email attachment file url
                    String attachmentFileUrl = temp.get("2").toString().replaceAll("\"","");
                    // 去除末尾&zw 并增加&disp=safe
                    attachmentFileUrl = attachmentFileUrl.replace("&zw","&disp=safe").replace("mail.google.com/mail/","mail.google.com/mail/u/0/");
                    gmailAttachmentInfoBOList.add(new GmailAttachmentInfoBO(attachmentFileName,attachmentFileType,attachmentFileUrl));
                }
                gmailEmailBO.setGmailAttachmentInfoBOList(gmailAttachmentInfoBOList);

            }
            list.add(gmailEmailBO);
        }
        logger.info("结束解析所有gmail邮件内容");
        return list;
    }


    public static void main(String[] args) {


    }
}
